﻿/*
 * 跟业务没有关系的公共函数库

 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace CommonLib.ComMon
{
    /// <summary>
    /// 
    /// </summary>
    public class CommonCode
    {
        /// <summary>
        /// 
        /// </summary>
        public CommonCode()
        {

        }

        /// <summary>
        /// 空行显示gridview标题,在datatable中没有数据的情况下,把绑定的Gridview的标题显示出来

        /// </summary>
        /// <param name="BindDT">绑定的数据表</param>
        /// <param name="BindGrid">绑定的gridview</param>
        public static void DataNullRow(System.Data.DataTable BindDT, GridView BindGrid)
        {
            BindDT.Rows.Add(BindDT.NewRow());
            BindGrid.DataSource = BindDT;
            BindGrid.DataBind();
            int columnCount = BindGrid.Rows[0].Cells.Count;
            BindGrid.Rows[0].Cells.Clear();
            BindGrid.Rows[0].Cells.Add(new TableCell());
            BindGrid.Rows[0].Cells[0].ColumnSpan = columnCount;
            BindGrid.Rows[0].Cells[0].Text = "";
            BindDT.Rows.Clear();
        }

        /// <summary>
        ///  空行显示SmartGridView标题,在datatable中没有数据的情况下,把绑定的SmartGridView的标题显示出来(标题下面多加一个空行)
        /// </summary>
        /// <param name="BindDT">绑定的数据表</param>
        /// <param name="BindGrid">绑定的SmartGridView</param>
        public static void DataNullRow(System.Data.DataTable BindDT, YYControls.SmartGridView BindGrid)
        {
            BindDT.Rows.Add(BindDT.NewRow());
            BindGrid.DataSource = BindDT;
            BindGrid.DataBind();
            int columnCount = BindGrid.Rows[0].Cells.Count;
            BindGrid.Rows[0].Cells.Clear();
            BindGrid.Rows[0].Cells.Add(new TableCell());
            BindGrid.Rows[0].Cells[0].ColumnSpan = columnCount;
            BindGrid.Rows[0].Cells[0].Text = "";
            BindDT.Rows.Clear();
        }

        /// <summary>
        ///  空行显示SmartGridView标题,在datatable中没有数据的情况下,仅仅显示绑定的SmartGridView的标题(标题下面没有空行)
        /// </summary>
        /// <param name="BindDT">绑定的数据表</param>
        /// <param name="BindGrid">绑定的SmartGridView</param>
        public static void DataNoneRow(System.Data.DataTable BindDT, GridView BindGrid)
        {
            BindDT.Rows.Add(BindDT.NewRow());
            BindGrid.DataSource = BindDT;
            BindGrid.DataBind();
            int columnCount = BindGrid.Rows[0].Cells.Count;
            BindGrid.Rows[0].Cells.Clear();
            BindGrid.Rows[0].Cells.Add(new TableCell());
            BindGrid.Rows[0].Cells[0].ColumnSpan = columnCount;
            BindGrid.Rows[0].Cells[0].Text = "";
            BindGrid.Rows[0].Visible = false;
            BindDT.Rows.Clear();
        }

        /// <summary>
        ///  空行显示SmartGridView标题,在datatable中没有数据的情况下,仅仅显示绑定的SmartGridView的标题(标题下面没有空行)
        /// </summary>
        /// <param name="BindDT">绑定的数据表</param>
        /// <param name="BindGrid">绑定的SmartGridView</param>
        public static void DataNoneRow(System.Data.DataTable BindDT, YYControls.SmartGridView BindGrid)
        {
            BindDT.Rows.Add(BindDT.NewRow());
            BindGrid.DataSource = BindDT;
            BindGrid.DataBind();
            int columnCount = BindGrid.Rows[0].Cells.Count;
            BindGrid.Rows[0].Cells.Clear();
            BindGrid.Rows[0].Cells.Add(new TableCell());
            BindGrid.Rows[0].Cells[0].ColumnSpan = columnCount;
            BindGrid.Rows[0].Cells[0].Text = "";
            BindGrid.Rows[0].Visible = false;
            BindDT.Rows.Clear();
        }

        /// <summary>
        /// 刷新gridview的空行

        /// </summary>
        /// <param name="BindGrid">gridview控件名称</param>
        /// <param name="NotNullColStr">非空列号组成的字符串，以逗号隔开</param>
        public static void RefreshNullGridView(YYControls.SmartGridView BindGrid, string NotNullColStr)
        {
            bool tmpBj = true;
            string[] ColArray = NotNullColStr.Split();
            if (BindGrid.Rows.Count == 1)
            {
                for (int i = 0; i < ColArray.Length; i++)
                {
                    if (!BindGrid.Rows[0].Cells[Convert.ToInt16(ColArray[i].ToString())].Text.Equals(""))
                    {
                        tmpBj = false;
                        break;
                    }
                }
                if (tmpBj == true)//说明非空字段的值为空，所以清除行。

                {
                    //  BindGrid.Rows[0].Controls.Clear();
                    BindGrid.Rows[0].Cells.Clear();
                    BindGrid.Rows[0].Cells.Add(new TableCell());
                    BindGrid.Rows[0].Cells[0].ColumnSpan = BindGrid.Rows[0].Cells.Count;
                    BindGrid.Rows[0].Cells[0].Text = "";
                }
            }
        }

        /// <summary>
        /// 刷新gridview的空行

        /// </summary>
        /// <param name="BindDT">gridview绑定的table</param>
        /// <param name="BindGrid">gridview控件名称</param>
        /// <param name="NotNullColStr">非空列号组成的字符串，以逗号隔开</param>
        public static void RefreshNullGridView(System.Data.DataTable BindDT, YYControls.SmartGridView BindGrid, string NotNullColStr)
        {
            bool tmpBj = true;
            string[] ColArray = NotNullColStr.Split();
            if (BindGrid.Rows.Count == 1)
            {
                for (int i = 0; i < ColArray.Length; i++)
                {
                    if (!BindGrid.Rows[0].Cells[Convert.ToInt16(ColArray[i].ToString())].Text.Equals(""))
                    {
                        tmpBj = false;
                        break;
                    }
                }
                if (tmpBj == true)//说明非空字段的值为空，所以清除行。

                {
                    DataNullRow(BindDT, BindGrid);
                }
            }
        }

        /// <summary>
        /// 把bool类型的值转换为　"0"和"1"
        /// </summary>
        /// <param name="TransBool"></param>
        /// <returns></returns>
        public static string ConvertBool(bool TransBool)
        {
            if (TransBool == true)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 获得gridview的条件。

        /// </summary>
        /// <param name="TransSGridW">GridView名称</param>
        /// <param name="ColNoStr">条件字段所在列号</param>
        /// <param name="CheckBoxName">checkbox名称</param>
        /// <returns></returns>
        public static DataTable GetCondTableCB(YYControls.SmartGridView TransSGridW, string ColNoStr, string CheckBoxName)
        {
            DataTable ResultDT = new DataTable();
            int i = 0;
            string[] ColNoArr;
            ColNoArr = ColNoStr.Split(',');
            for (i = 0; i < ColNoArr.Length; i++)
            {
                ResultDT.Columns.Add("Cond" + i.ToString(), typeof(string));
            }
            ResultDT.Columns.Add("row", typeof(string));

            for (i = 0; i < TransSGridW.Rows.Count; i++)
            {
                if (TransSGridW.Rows[i].FindControl(CheckBoxName) != null)
                {
                    if (((CheckBox)TransSGridW.Rows[i].FindControl(CheckBoxName)).Checked == true)
                    {
                        DataRow TmpDR = ResultDT.NewRow();

                        for (int j = 0; j < ColNoArr.Length; j++)
                        {
                            TmpDR[j] = TransSGridW.Rows[i].Cells[Convert.ToInt16(ColNoArr[j])].Text;
                        }
                        TmpDR[ColNoArr.Length] = i.ToString();
                        ResultDT.Rows.Add(TmpDR);
                    }
                }
            }
            return ResultDT;
        }


        /// <summary>
        /// 获得gridview的第一个条件。

        /// </summary>
        /// <param name="TransSGridW">GridView名称</param>
        /// <param name="ColNoStr">条件字段所在列号</param>
        /// <param name="CheckBoxName">checkbox名称</param>
        /// <returns></returns>
        public static DataTable GetCondTableFirst(YYControls.SmartGridView TransSGridW, string ColNoStr, string CheckBoxName)
        {
            DataTable ResultDT = new DataTable();
            int i = 0;
            string[] ColNoArr;
            ColNoArr = ColNoStr.Split(',');
            for (i = 0; i < ColNoArr.Length; i++)
            {
                ResultDT.Columns.Add("Cond" + i.ToString(), typeof(string));
            }
            ///增加返回该列行号
            ResultDT.Columns.Add("row", typeof(string));
            for (i = 0; i < TransSGridW.Rows.Count; i++)
            {
                if (TransSGridW.Rows[i].FindControl(CheckBoxName) != null)
                {
                    if (((CheckBox)TransSGridW.Rows[i].FindControl(CheckBoxName)).Checked == true)
                    {
                        DataRow TmpDR = ResultDT.NewRow();

                        for (int j = 0; j < ColNoArr.Length; j++)
                        {
                            TmpDR[j] = TransSGridW.Rows[i].Cells[Convert.ToInt16(ColNoArr[j])].Text;
                        }
                        TmpDR[ColNoArr.Length] = i.ToString();
                        ResultDT.Rows.Add(TmpDR);
                        break;
                    }
                }
            }
            return ResultDT;
        }


        /// <summary>
        /// 获得gridview的第一个条件。

        /// </summary>
        /// <param name="TransSGridW">GridView名称</param>
        /// <param name="ColNoStr">条件字段所在列号</param>
        /// <param name="RadioButtonName">RadioButton名称</param>
        /// <returns></returns>
        public static DataTable GetCondTableRB(YYControls.SmartGridView TransSGridW, string ColNoStr, string RadioButtonName)
        {
            DataTable ResultDT = new DataTable();
            int i = 0;
            string[] ColNoArr;
            ColNoArr = ColNoStr.Split(',');
            for (i = 0; i < ColNoArr.Length; i++)
            {
                ResultDT.Columns.Add("Cond" + i.ToString(), typeof(string));
            }
            ResultDT.Columns.Add("row", typeof(string));

            for (i = 0; i < TransSGridW.Rows.Count; i++)
            {
                if (((RadioButton)TransSGridW.Rows[i].FindControl(RadioButtonName)).Checked == true)
                {
                    DataRow TmpDR = ResultDT.NewRow();

                    for (int j = 0; j < ColNoArr.Length; j++)
                    {
                        TmpDR[j] = TransSGridW.Rows[i].Cells[Convert.ToInt16(ColNoArr[j])].Text;
                    }
                    TmpDR[ColNoArr.Length] = i.ToString();
                    ResultDT.Rows.Add(TmpDR);
                    break;
                }
            }
            return ResultDT;
        }


        /// <summary>
        /// 验证当前页码
        /// </summary>
        /// <param name="CurPage">当前页数</param>
        /// <param name="PageReCords">分页记录数</param>
        /// <param name="BtnType">0 首页，1 上一页，2 下一页，3 末页</param>
        /// <returns></returns>
        public static Int16 GetCurrPage(Int16 CurPage, Int16 PageReCords, Int16 RCord)
        {
            Int16 TotalPage = GetTotalPage(RCord, PageReCords);
            if (TotalPage > 0 && CurPage == 0)
                return 1;
            if (TotalPage < CurPage)
                return TotalPage;
            else
                return CurPage;
        }


        /// <summary>
        /// 验证当前页码
        /// </summary>
        /// <param name="CurPage">当前页数</param>
        /// <param name="PageReCords">分页记录数</param>
        /// <param name="BtnType">0 首页，1 上一页，2 下一页，3 末页</param>
        /// <returns></returns>
        public static int GetCurrPage(Int16 CurPage, int PageReCords, int RCord)
        {
            int TotalPage = GetTotalPage(RCord, PageReCords);
            if (TotalPage > 0 && CurPage == 0)
                return 1;
            if (TotalPage < CurPage)
                return TotalPage;
            else
                return CurPage;
        }


        /// <summary>
        /// 获得所有页
        /// </summary>
        /// <param name="PageRecords"></param>
        /// <returns></returns>
        public static Int16 GetTotalPage(Int16 RCord, int PageRecords)
        {
            Int16 TotalRecord = RCord;
            Int16 YCount = Convert.ToInt16(TotalRecord % PageRecords); //取模
            Int16 RCount = Convert.ToInt16(TotalRecord / PageRecords); //取余
            if (YCount > 0)
                RCount += 1;
            return RCount;
        }


        /// <summary>
        /// 获得所有页
        /// </summary>
        /// <param name="PageRecords"></param>
        /// <returns></returns>
        public static int GetTotalPage(int RCord, int PageRecords)
        {
            int TotalRecord = RCord;
            int YCount = Convert.ToInt16(TotalRecord % PageRecords); //取模
            int RCount = Convert.ToInt16(TotalRecord / PageRecords); //取余
            if (YCount > 0)
                RCount += 1;
            return RCount;
        }

        /// <summary>
        /// GridView绑定RadioButton
        /// </summary>
        /// <param name="e"></param>
        /// <param name="RadioBName"></param>
        public static void SetGridDataBound(GridViewRowEventArgs e, string RadioBName)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl(RadioBName);
                rb.Attributes.Add("onclick", "RCheckId(this)");//给RadioButton绑定onclick
            }
        }

        public static string GetBytesStr(object Bytes)
        {
            string RtnStr = "", tmpStr = "";
            for (int i = 0; i < (Bytes as System.Byte[]).Length; i++)
            {
                tmpStr = (Bytes as System.Byte[])[i].ToString("X");
                if (tmpStr.Length == 1)
                {
                    tmpStr = "0" + tmpStr;
                }
                RtnStr = RtnStr + tmpStr;
            }
            return RtnStr;
        }

      

    }
}
